define(function(require) {


	require('../../../c/base/index');

	var feedbackListTpl = require('../../../c/nav/index');

	require('./feedback-list-tpl');

	require('./dialog');

	require('./lightbox')

	// require('./index.css');

	var defaultData = require('./data');


	$('<a href="http://h5.m.52shangou.com/feedback/index.html" target="_blank" style="position:fixed;top:10px;right:10px;z-index:10000; color:#fff;">体验新版</a>').appendTo('.main-header');


	var feedbackListTpl = juicer.templates['feedback-list.juicer.html'];


	var method = {
		y: function(d) {
			return d.getFullYear()
		},
		M: function(d) {
			return d.getMonth() + 1
		},
		d: function(d) {
			return d.getDate()
		},
		H: function(d, halfMode) {
			var val = d.getHours();
			return halfMode ? val % 12 : val;
		},
		h: function(d) {
			return method.H(d, true)
		},
		m: function(d) {
			return d.getMinutes();
		},
		s: function(d) {
			return d.getSeconds();
		},
		w: (function() {
			var WeekName = ['日', '一', '二', '三', '四', '五', '六'];
			return function(d) {
				return WeekName[d.getDay()]
			}
		})()
	}

	function formatDate(oDate, sFormation) {
		if (sFormation && oDate && !isNaN(oDate.getTime())) {
			return sFormation.replace(/y+|M+|d+|H+|h+|m+|s+|w+/g, function(found) {
				var getter = method[found[0]],
					val = getter && getter(oDate);
				return found.length === 2 ? ("0" + val).slice(-2) : "" + val;
			});
		} else {
			return "";
		}
	}

	window.formatDate = formatDate;


	window.getPhoneNumInfoExtCallback = function(data){
		if(data.cityname && data.mobile){
			$('.J_Mobile' + data.mobile).html( data.mobile + '（' +data.cityname+ '）' )
		}
	}



	var PAGE_SIZE = 30;

	var elTbody = $('#J_Tbody');


	var pageIndex = 1;

	var FeedBack = {

		init: function() {
			//初始化菜单选中效果
			$(".fa-circle-o").each(function() {
				if ($(this).parents("a").text() == "用户反馈") {
					$(this).parents("a").parents("li").addClass("active");
				}
			});
			this.getData();
			this.viewMobileAddress();
			this.page();
			this.edit();
			this.department();
			this.handleStatus();
			this.viewDetail();
		},
		getData: function() {


			// $.dialog({
			// 	types:"loading",
			// 	contents :"loading",
			// 	width:200,
			// 	height:100
			// });

			$.ajax({
				url: '/member/getUserCommentList.do',
				data:{
					pageIndex:pageIndex,
					pageSize:PAGE_SIZE
				},
				success: function(data) {
					FeedBack.callback(data);
					// $.dialog({types:"closeFun"});
				}
			})
		},
		page:function(){
			$('.J_Page').on('click',function(e){
				var target = $(e.currentTarget);
				if(target.hasClass('J_LastPage')){
					pageIndex--;
				}else{
					pageIndex++;
				}
				if(pageIndex < 1){
					pageIndex = 1;
				}
				FeedBack.getData();

				window.scrollTo(0,50);

				return;
			})
		},
		callback:function(data){
			if(data.status && data.entry){
				FeedBack.renderHtml(data.entry);
			}
		},
		renderHtml:function(data){
			var list = this.formatData(data);
			var html = feedbackListTpl({list:list,defaultData:defaultData});
			elTbody.html(html);
		},
		formatData: function(list) {
			for (var i = 0, len = list.length; i < len; i++) {
				var li = list[i];
				li.time = formatDate(new Date(li.gmtCreate),'yyyy-MM-dd HH:mm:ss');
				this.setUa(li);
				li.appVersion = this.getAppVersion(li.remoteUserAgent);
				li.mobile = this.getMobile(li.remoteUserAgent);
			}
			return list;
		},
		setUa:function(item){

			if(!item.remoteUserAgent && item.comment.indexOf('<<==SDG==>>') > -1){
				var spComment = item.comment.split('<<==SDG==>>');
				item.remoteUserAgent = spComment[1];
				item.comment = spComment[0]; 
			}
		},
		getAppVersion:function(ua){
			var version = '';
			if(ua){
				version =  ua.match(/appVersion\((.*?)\)/) && ua.match(/appVersion\((.*?)\)/)[1];
			}
			return version;
		},
		getMobile:function(ua){
			var mobile = '';
			// debugger;
			if( ua && ua.indexOf('iPhone') > -1){
				var systemVersion = ua.match(/systemVersion\((.*?)\)/) && ua.match(/systemVersion\((.*?)\)/)[1] || '' ;
				mobile = 'iOS ' + systemVersion;
			}else if(ua && ua.indexOf('Android') > -1){
				var mobile = ua.match('(Android.*?)Build') && ua.match('(Android.*?)Build')[1] || '';
				var mobileArr = mobile.split(';');
				if(mobileArr.length == 3){
					mobile = mobileArr[0] + mobileArr[2];
				}
			}
			return mobile;
		},
		viewDetail:function(){
			$(document).delegate('.J_ViewDetail','click',function(e){
				var target = $(e.currentTarget);
				target.siblings('.J_UserDetail').removeClass('hidden');
				target.remove();
				// target.find('a').html('收起详情&#xe601;')
			});

			// 				<a href="javascript:void(0);" class="feedback-iconfont J_HiddenDetail hidden"></a>

		},
		viewMobileAddress:function(){
			$('#J_ViewTelFrom').on('click',function(){
				$('.J_Mobile').each(function(k,v){
					v  = $(v);
					var mobile = v.attr('data-mobile');

					$.ajax({
						url:'http://virtual.paipai.com/extinfo/GetMobileProductInfo?callname=getPhoneNumInfoExtCallback&amount=10000&mobile='+mobile,
						// jsonp:'callname',
						dataType:'jsonp',
						success:function(data){
							if(data.cityname){
								v.html ( mobile + '（' +data.cityname+ '）' );
							}
						}
					})
				});
			})
		},
		department:function(){
			$(document).delegate('.J_Department','change',function(e){
				var target = $(e.currentTarget);
				var commentId  = target.attr('data-id');
				var changeId = target.val();

				$.ajax({
					url: '/member/updateUserComment.do',
					contentType: 'application/json',
					data:JSON.stringify({
						id:commentId,
						department:changeId
					}),
					method:'post',
					type:'post',
					success: function(data) {
						
					}
				});

			});
		},
		handleStatus:function(){
			$(document).delegate('.J_HandleStatus','change',function(e){
				var target = $(e.currentTarget);
				var commentId  = target.attr('data-id');
				var changeId = target.val();

				var elTr = $('#J_CommentTr'+commentId);
				$.ajax({
					url: '/member/updateUserComment.do',
					contentType: 'application/json',
					data:JSON.stringify({
						id:commentId,
						handleStatus:changeId
					}),
					method:'post',
					type:'post'
				});


				if(changeId == 0){
					elTr.removeClass('comment-done');
				}else{
					elTr.addClass('comment-done');
				}

			});
		},
		edit:function(){
			$(document).delegate('.J_Edit','click',function(e){

				var target = $(e.currentTarget);
				var currentMark = target.siblings('span');
				var id = target.attr('data-id');

				$.dialog({
					types : 'prompt',
					titles : '填写备注',
					// contents : '<p>请填写备注:</p>',
					width : 400,
					height : 240,
					p : 'test',
					default:currentMark.html(),
					callback : function(str){
						$.ajax({
							url: '/member/updateUserComment.do',
							contentType: 'application/json',
							data:JSON.stringify({
								id:id,
								handleResult:str
							}),
							method:'post',
							type:'post',
							success: function(data) {
								currentMark.html(str)
							}
						});

						currentMark.html(str);
					}

				});


			})
		}
	};


	FeedBack.init();
});